﻿using System.Collections.Generic;
using UnityEngine;
using WooEngine.Data.Interfaces;

namespace WooEngine.Data.Base
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="K">key 存储单对象索引</typeparam>
    /// <typeparam name="V">存储的单对象值</typeparam>
    /// <typeparam name="D">初始化时候需要传入的数据类型</typeparam>
    /// <typeparam name="O">单个对象的初始化数据类型</typeparam>
    public abstract class AbstractData<K, V, D, O> : IData<D> where V : AbstractObjData<K,O>
    {
        public Dictionary<K, V> data_dic ;

        public AbstractData()
        {
            if (this.data_dic == null)
            {
                this.data_dic = new Dictionary<K, V>();
            }
        }


        public void Initialize(D initData)
        {
            this.InitializePre();
            if (this.data_dic.Count > 0)
            {
                foreach (K key in data_dic.Keys)
                {
                    data_dic[key].Initialize(GetInitDataValue(key));
                };
            }
        }

        protected abstract O GetInitDataValue(K key);

        protected virtual void InitializePre()
        {

        }

        public void Release()
        {
            if (this.data_dic != null)
            {
                foreach (V data in data_dic.Values)
                {
                    data.Release();
                }
                data_dic.Clear();
            }
            releaseOther();
        }

        protected virtual void releaseOther()
        {

        }

    }
}